/**
 * Created by lizhe on 2018/5/14.
 */
$(document).ready(function(){
    //新增
    $("#add_Label").click(function(){
        document.getElementById("labelContent").innerHTML = document.getElementById("labelContent").innerHTML +
            '<div class="labelDiv">'+
            '<span class="hidden"></span>'+
            '<input type="radio" name="label">'+
            '<input type="text" class="inputText" style="width: 300px;display: inline;margin-left: 15px">'+
            '<span style="display: none"></span>'+
            '<a style="margin-left: 10px" onclick="labelSave(this)">保存</a>'+
            '</div>'
    });

    //修改
    $("#edit_Label").click(function(){
        var div = document.getElementById("labelContent").getElementsByTagName("div");
        var x = 0;
        for(var i = 0;i<div.length;i++){
            if(i == 0){
                if(div[i].getElementsByTagName("input")[0].checked == true){
                    div[i].getElementsByTagName("input")[1].style.display = "inline";
                    div[i].getElementsByTagName("span")[1].style.display = "none";
                    div[i].getElementsByTagName("a")[0].style.display = "";
                    x = 1;
                    break;
                }
            }else{
                if(div[i].getElementsByTagName("input")[0].checked == true){
                    div[i].getElementsByTagName("input")[1].style.display = "inline";
                    div[i].getElementsByTagName("span")[1].style.display = "none";
                    div[i].getElementsByTagName("a")[0].style.display = "";
                    div[i].getElementsByTagName("a")[1].style.display = "";
                    x = 1;
                    break;
                }
            }
        }
        if(x == 0){
            modalinfo("请选择标签");
        }
    });

    //删除
    $("#delete_Label").click(function(){
        var div = document.getElementById("labelContent").getElementsByTagName("div");
        var id;
        var x = 0;
        for(var i = 0;i<div.length;i++){
            if(div[i].getElementsByTagName("input")[0].checked == true){
                id = div[i].getElementsByTagName("span")[0].innerHTML;
                x = 1;
                document.getElementById("delLabel_id").innerHTML = id;
                showmodal("#deleteLabelmodal");
                break;
            }
        }
        if(x == 0){
            modalinfo("请选择标签");
        }
    });

    //新增
    $("#sAdd_label").click(function(){
        document.getElementById("sLabelContent").innerHTML = document.getElementById("sLabelContent").innerHTML +
            '<div class="labelDiv">'+
            '<span class="hidden"></span>'+
            '<input type="radio" name="label">'+
            '<input type="text" class="inputText" style="width: 225px;display: inline;margin-left: 15px">'+
            '<span style="display: none"></span>'+
            '<a style="margin-left: 10px" onclick="labelSave(this)">保存</a>'+
            '</div>'
    });

    //修改
    $("#sEdit_label").click(function(){
        var div = document.getElementById("sLabelContent").getElementsByTagName("div");
        var x = 0;
        for(var i = 0;i<div.length;i++){
            if(i == 0){
                if(div[i].getElementsByTagName("input")[0].checked == true){
                    div[i].getElementsByTagName("input")[1].style.display = "inline";
                    div[i].getElementsByTagName("span")[1].style.display = "none";
                    div[i].getElementsByTagName("a")[0].style.display = "";
                    x = 1;
                    break;
                }
            }else{
                if(div[i].getElementsByTagName("input")[0].checked == true){
                    div[i].getElementsByTagName("input")[1].style.display = "inline";
                    div[i].getElementsByTagName("span")[1].style.display = "none";
                    div[i].getElementsByTagName("a")[0].style.display = "";
                    div[i].getElementsByTagName("a")[1].style.display = "";
                    x = 1;
                    break;
                }
            }
        }
        if(x == 0){
            modalinfo("请选择标签");
        }
    });

    //删除
    $("#sDelete_label").click(function(){
        var div = document.getElementById("sLabelContent").getElementsByTagName("div");
        var id;
        var x = 0;
        for(var i = 0;i<div.length;i++){
            if(div[i].getElementsByTagName("input")[0].checked == true){
                id = div[i].getElementsByTagName("span")[0].innerHTML;
                x = 1;
                document.getElementById("delLabel_id").innerHTML = id;
                showmodal("#deleteLabelmodal");
                break;
            }
        }
        if(x == 0){
            modalinfo("请选择标签");
        }
    });

    //确认删除
    $("#delLabel_confirm").click(function(){
        var id = document.getElementById("delLabel_id").innerHTML;
        $.ajax({
            url : urladdr + "/web/article/deleteLabels",
            method : "POST",
            data : {
                token:Mytoken,
                labelId : id
            },
            success : function(data){
                if(data.code == 0){
                    modalclose("#deleteLabelmodal");
                    setTimeout(function(){
                        modalinfo("删除成功");
                    },400);
                    queryLabel(ZtreeId);
                }else if(data.code == 401){
                    location.href = "login.html"
                }else{
                    modalinfo(data.msg);
                }
            }
        });
    });

    //确定
    $("#addLabel_confirm").click(function(){
        var div = document.getElementById("labelContent").getElementsByTagName("div");
        var id;
        var x = 0;
        for(var i = 0;i<div.length;i++){
            if(div[i].getElementsByTagName("input")[0].checked == true){
                id = div[i].getElementsByTagName("span")[0].innerHTML;
                var name = div[i].getElementsByTagName("span")[1].innerHTML;
                x = 1;
                allHide();
                $("#toAdd").click();
                document.getElementById("add_name").innerHTML = "<span class='import'>*</span>姓名";
                document.getElementById("add_title").placeholder = "姓名";
                document.getElementById("add_topTitle").innerHTML = "新增教师风采";
                document.getElementById("add_imgTitle").innerHTML = "<span class='import'>*</span>图片上传";
                document.getElementById("add-moto").style.display = "";
                document.getElementById("add-position").style.display = "";
                document.getElementById("add-priority").style.display = "";
                document.getElementById("addLabels").style.display = "";
                document.getElementById("add_contentTitle").style.display = "none";
                document.getElementById("edit").style.display = "none";
                document.getElementById("uploadImg").setAttribute("class", "uploadImg3");
                document.getElementById("uploadImg").src = "images/teacherAdd.png";
                document.getElementById("add_imgUrl").innerHTML = "images/teacherAdd.png";

                document.getElementById("add-imgDel").style.marginRight = "34px";

                document.getElementById("addLabel_id").innerHTML = id;
                document.getElementById("addLabel").innerHTML = name;
                break;
            }
        }
        if(x == 0){
            modalinfo("请选择标签");
            //showmodal("#selectLabelmodal")
        }
    });

    $("#sLabel_confirm").click(function(){
        var type = document.getElementById("labelType").innerHTML;
        var str = document.getElementById("sLabelContent").getElementsByTagName("div");
        for(var i = 0;i<str.length;i++){
            if(str[i].getElementsByTagName("input")[0].checked == true){
                var id = str[i].getElementsByTagName("span")[0].innerHTML;
                var name = str[i].getElementsByTagName("span")[1].innerHTML;
                break;
            }
        }
        if(type == "1"){
            document.getElementById("addLabel_id").innerHTML = id;
            document.getElementById("addLabel").innerHTML = name;
        }else{
            document.getElementById("editLabel_id").innerHTML = id;
            document.getElementById("editLabel").innerHTML = name;
        }
        modalclose("#selectLabelmodal");
    });
});

//获取标签
function queryLabel(menuId){
    $.ajax({
        url : urladdr + "/web/article/queryLabels",
        method : "GET",
        data : {
            token:Mytoken,
            menuId : menuId
        },
        success : function(data){
            if(data.code == 0){
                $("#labelContent").empty();
                $.each(data.data,function(i,date){
                    if(i==0){
                        document.getElementById("labelContent").innerHTML = document.getElementById("labelContent").innerHTML +
                            '<div class="labelDiv">'+
                            '<span class="hidden">'+date.id+'</span>'+
                            '<input type="radio" name="label" style="">'+
                            '<input type="text" class="inputText" style="width: 300px;display: none;margin-left: 10px" value="'+date.labelName+'">'+
                            '<span style="margin-left: 15px">'+date.labelName+'</span>'+
                            '<a style="margin-left: 10px;display: none" onclick="labelSave(this)">保存</a>'+
                            '</div>'
                    }else{
                        document.getElementById("labelContent").innerHTML = document.getElementById("labelContent").innerHTML +
                            '<div class="labelDiv">'+
                            '<span class="hidden">'+date.id+'</span>'+
                            '<input type="radio" name="label" style="">'+
                            '<input type="text" class="inputText" style="width: 300px;display: none;margin-left: 10px" value="'+date.labelName+'">'+
                            '<span style="margin-left: 15px">'+date.labelName+'</span>'+
                            '<a style="margin-left: 10px;display: none" onclick="labelSave(this)">保存</a>'+
                            '<a style="margin-left: 10px;" onclick="labelMoveUp(this)">上移</a>'+
                            '</div>'
                    }
                });

                $("#sLabelContent").empty();
                $.each(data.data,function(i,date){
                    if(i==0){
                        document.getElementById("sLabelContent").innerHTML = document.getElementById("sLabelContent").innerHTML +
                            '<div class="labelDiv">'+
                            '<span class="hidden">'+date.id+'</span>'+
                            '<input type="radio" name="label" style="">'+
                            '<input type="text" class="inputText" style="width: 240px;display: none;margin-left: 10px" value="'+date.labelName+'">'+
                            '<span style="margin-left: 15px">'+date.labelName+'</span>'+
                            '<a style="margin-left: 10px;display: none" onclick="labelSave(this)">保存</a>'+
                            '</div>'
                    }else{
                        document.getElementById("sLabelContent").innerHTML = document.getElementById("sLabelContent").innerHTML +
                            '<div class="labelDiv">'+
                            '<span class="hidden">'+date.id+'</span>'+
                            '<input type="radio" name="label" style="">'+
                            '<input type="text" class="inputText" style="width: 240px;display: none;margin-left: 10px" value="'+date.labelName+'">'+
                            '<span style="margin-left: 15px">'+date.labelName+'</span>'+
                            '<a style="margin-left: 10px;display: none" onclick="labelSave(this)">保存</a>'+
                            '<a style="margin-left: 10px;" onclick="labelMoveUp(this)">上移</a>'+
                            '</div>'
                    }
                });
            }else if(data.code == 401){
                location.href = "login.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}

//标签保存
function labelSave(obj){
    var id = obj.parentElement.getElementsByTagName("span")[0].innerHTML;
    var title = obj.parentElement.getElementsByTagName("input")[1].value;
    if(title==""){
        modalinfo("请输入标签名称");
    }else{
        if(id==""){  //新建
            var data = {
                token : Mytoken,
                menuId : ZtreeId,
                labelName : title
            };
            $.ajax({
                url : urladdr + "/web/article/addLabel",
                method : "POST",
                data : data,
                success : function(data){
                    if(data.code == 0){
                        modalinfo("新建成功");
                        queryLabel(ZtreeId);
                    }else if(data.code == 401){
                        location.href = "login.html"
                    }else{
                        modalinfo(data.msg);
                    }
                }
            });
        }else{  //修改
            data = {
                token : Mytoken,
                labelName : title,
                labelId : id
            };
            $.ajax({
                url : urladdr + "/web/article/updateLabel",
                method : "POST",
                data : data,
                success : function(data){
                    if(data.code == 0){
                        modalinfo("修改成功");
                        queryLabel(ZtreeId);
                    }else if(data.code == 401){
                        location.href = "login.html"
                    }else{
                        modalinfo(data.msg);
                    }
                }
            });
        }
    }
}

//标签上移
function labelMoveUp(obj){
    var id = obj.parentElement.getElementsByTagName("span")[0].innerHTML;
    $.ajax({
        url : urladdr + "/web/article/updateLabelPriority",
        method : "POST",
        data : {
            token : Mytoken,
            labelId : id,
            menuId : ZtreeId
        },
        success : function(data){
            if(data.code == 0){
                queryLabel(ZtreeId);
            }else if(data.code == 401){
                location.href = "login.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}

function selectLabel(obj,type){
    showmodal("#selectLabelmodal");
    document.getElementById("labelType").innerHTML = type;
    var str = document.getElementById("sLabelContent").getElementsByTagName("div");
    if(type == "1"){
        var oldId = document.getElementById("addLabel_id").innerHTML;
        queryLabel(ZtreeId);
    }else{
        var menuId = document.getElementById("edit_menuId").innerHTML;
        oldId = document.getElementById("editLabel_id").innerHTML;
        queryLabel(menuId);
    }
    setTimeout(function(){
        for(var i = 0;i<str.length;i++){
            if(str[i].getElementsByTagName("span")[0].innerHTML == oldId){
                str[i].getElementsByTagName("input")[0].checked = true;
                break;
            }
        }
    },400);
}

function teacherPage(page,pageSize,menuId){
    var title = document.getElementById("search_title").value;
    var creatorName = document.getElementById("search_name").value;
    var status = document.getElementById("search_status").value;
    var begin = document.getElementById("search_begin").value;
    var end = document.getElementById("search_end").value;
    var d1 = parseInt(getTime(begin));
    var d2 = parseInt(getTime(end));
    if(begin == "" && end !=""){
        modalinfo("请输入起始日期");
        return false;
    }else if(begin!="" && end == ""){
        modalinfo("请输入截止日期");
        return false;
    }else if(d1>d2){
        modalinfo("起始日期不能大于截止日期");
        return false;
    }else{
        var data = {
            token:Mytoken,
            page:page,
            pageSize:pageSize,
            menuId:menuId,
            status:status
        };
        if(title!=""){
            data.title = title;
        }
        if(creatorName != ""){
            data.creatorName = creatorName;
        }
        if(begin!="" && end!=""){
            data.begin = begin;
            data.end = end;
        }
        $.ajax({
            url : urladdr + "/web/article/teacherPage",
            method : "GET",
            data : data,
            async:false,
            success : function(data){
                if(data.code == 0){
                    $("#article_table").empty();
                    $.each(data.data.list,function(i,date){
                        var color = "";
                        if(date.status == 1){
                            color = "blueColor";
                        }else if(date.status == 2){
                            color = "greenColor";
                        }else if(date.status == 3){
                            color = "redColor";
                        }else if(date.status == 4){
                            color = "brownColor";
                        }
                        if(date.content.length>20){
                            var content = date.content;
                            content = content.substring(0,20)+"···";
                        }else{
                            content = date.content
                        }
                        var num = (page -1)*15 + (i+1);
                        var menuName;
                        for(var j = 0;j < datas.length; j++){
                            if(date.menuId == datas[j].id){
                                menuName = datas[j].name;
                                break;
                            }
                        }
                        var dateTime = date.addTime;
                        if(dateTime!=null){
                            dateTime = dateTime.split(" ");
                            var time = dateTime[0];
                        }
                        document.getElementById("article_table").innerHTML = document.getElementById("article_table").innerHTML +
                            '<tr>'+
                            '<td class="hidden">'+date.id+'</td>' +
                            '<td style="width:30px"><input type="checkbox"></td>'+
                            '<td style="width: 50px">'+num+'</td>'+
                            '<td>'+date.title+'('+date.labelName+')</td>' +
                            '<td>'+menuName+'</td>'+
                            '<td>'+type[date.type]+'</td>' +
                            '<td>'+date.userName+'</td><td class='+color+'>'+statu[date.status]+'</td>'+
                            '<td style="width: 200px">'+time+'</td>'+
                            '<td style="width: 200px">'+date.addTime+'</td>'+
                            '<td>'+date.hit+'</td>'+
                            '<td><a onclick="detail('+i+')">详情</a></td>'+
                            '<td class="hidden">'+date.status+'</td>'+
                            '</tr>'
                    });
                    $("#page").pagination({
                        pages: data.data.totalPage,
                        cssStyle: 'light-theme',
                        displayedPages:5,
                        edges:2,
                        currentPage:page
                    });
                    document.getElementById("recordCount").innerHTML = data.data.totalCount;
                    document.getElementById("pageCount").innerHTML = data.data.totalPage;
                    clicktable('article_table');
                }else{
                    modalinfo(data.msg);
                }
            }
        });
    }
}